Load libraries

library(leaflet)
library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Project Summary

Read 2017 Car crash data in Allegheny County.

DF<-read.csv("CarCrash2017.csv")

Interactive MAP - Total car crash by month

Subset the location of the car crash Keep the month of the crash Create a new column with the months names instead of a number Since there is a large number of the crash There will be an option to show car crash per specific month For informaition to present in popup : AUTOMOBILE_COUNT - the number of cars involved DRINKING_DRIVER - Alcohol involved ? FATAL_COUNT - Total number of death ILLEGAL_DRUG_RELATED - Ilegal drug involvment

dfCrash<-select(DF,DEC_LAT,DEC_LONG,CRASH_MONTH,AUTOMOBILE_COUNT,DRINKING_DRIVER,FATAL_COUNT,ILLEGAL_DRUG_RELATED)
colnames(dfCrash)<-c("latitude","longitude","Month Crash","No.Cars.Involved","Driniking.Driver","Death.Count","Drug.Involved")
dfCrash<-dfCrash %>% drop_na()
dfCrash<-mutate(dfCrash,Month.Crash.Name=month.name[dfCrash$`Month Crash`])

Use a PNG file as an icon for markers on the MAP

CarsIcon <- makeIcon("CarsIcon1.png",iconWidth = 45, iconHeight = 45)

Helper function to add markers

####################################
# Add_Marker --- Helper function   #
#Paramters : 
#my_map - leaflet object - markeers will be added to this object 
#lat_ln_data - Data frame with the lat and long (of the required markers 
#groupname - The group name of the markers
#popup - Popup content 
#IconMarker - The icon of the marker

Add_Marker <- function(my_map,lat_ln_data,groupname,content,iconMarker=CarsIcon) {
  
  addMarkers(my_map,data=lat_ln_data,clusterOptions = markerClusterOptions(),group =groupname,icon = iconMarker,popup = paste("Car involved:", content$No.Cars.Involved ,"<br>",
             "Fatality:",content$Death.Count,"<br>",
              "Drugs Involved :", content$Drug.Involved,"<br>",
               " Alcohol involved:",content$Driniking.Driver))
  #addPopups(my_map, lat_ln_data$latitude,lat_ln_data$longitude,content)
  
}
latitude<-c(40.672842,40.668266,40.549080,40.521991,40.421757,40.392743,40.291685,40.228147,40.194802,40.229574,40.255097,40.331806,40.477968,40.597106,40.607889,40.615364)

longitude<-c(-80.148578,-79.717074,
             -79.770777, -79.707223,-79.711924,-79.765167,-79.789453,-79.796669,-79.89504,-79.974788,-79.920311, -80.182883, -80.364246,-80.195354,-80.176635,-80.146614)

2017 Car crash data of Allegheny County